package cn.leyaber.hrm.controller;


import cn.leyaber.hrm.util.AjaxResult;
import cn.leyaber.hrm.redusutils.RedisUtils;
import org.springframework.web.bind.annotation.*;

//缓存
@RestController
@RequestMapping("/redis")
public class RedisController {

    //设置值 : 请求方式： xxx/set?key=xxx&value=xxxx
    @RequestMapping(value = "/set",method = RequestMethod.POST)
    public AjaxResult set(@RequestParam("key") String key  ,
                          @RequestParam("value")String value){
        RedisUtils.INSTANCE.set(key,value);
        return AjaxResult.me();
    }
    @RequestMapping(value = "/setex",method = RequestMethod.POST)
    public AjaxResult setex(@RequestParam("key") String key  ,
                            @RequestParam("seconds")int seconds,
                           @RequestParam("value")String value){
        RedisUtils.INSTANCE.set(key,seconds,value);
        return AjaxResult.me();
    }

    //取值 : get/xxx
    @RequestMapping(value = "/get/{key}",method = RequestMethod.GET)
    public AjaxResult get(@PathVariable("key") String key){
        String result = RedisUtils.INSTANCE.get(key);
        return AjaxResult.me().setResultObj(result);
    }
    //删除KEY
    @RequestMapping(value = "/del/{key}",method = RequestMethod.DELETE)
    public AjaxResult del(@PathVariable("key") String key){
        boolean result = RedisUtils.INSTANCE.del(key);
        return AjaxResult.me().setSuccess(result).setMessage(!result?"删除失败":"删除成功");
    }
}
